package com.jiayou.im.controller

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.jiayou.im.entity.Moment
import com.jiayou.im.entity.Relation
import com.jiayou.im.entity.Userinfo
import com.jiayou.im.service.impl.MomentServiceImpl
import com.jiayou.im.service.impl.RelationServiceImpl
import com.jiayou.im.service.impl.UserinfoServiceImpl
import com.jiayou.im.vo.MomentList
import com.jiayou.im.vo.Resoult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

/**
@author 李 双
@data   2020-11-24  13:33
@info
 */
@RestController
@RequestMapping("/im/moment")
open class MomentController {


    @Autowired
    private lateinit var momentServiceImpl: MomentServiceImpl

    @Autowired
    private lateinit var relationServiceImpl: RelationServiceImpl

    @Autowired
    private lateinit var userInFoServiceImpl: UserinfoServiceImpl


    //获取朋友圈列表
    @RequestMapping("getList")
    open fun getList(@RequestParam("id") id: String): Resoult<ArrayList<MomentList>> {
        val relationsList = relationServiceImpl.list(
            KtQueryWrapper(Relation::class.java).eq(Relation::id1, id).or().eq(Relation::id2, id)
        )
        val idList = ArrayList<Userinfo>()
        relationsList.forEach {
            val user = userInFoServiceImpl.getById(if (it.id1 == id) it.id2 else it.id1)
            if (user != null)
                idList.add(user)
        }


        val condition = idList.map { it.username } as ArrayList
        //当然也要把自己加进去
        condition.add(id)

        val list = momentServiceImpl.list(
            KtQueryWrapper(Moment::class.java)
                .`in`(Moment::username, condition)
                .orderByDesc(Moment::id)
        )

        val momentList = ArrayList<MomentList>()
        list.forEach {
            val obj = userInFoServiceImpl.getById(it.username)
            momentList.add(
                MomentList(
                    id = it.id!!,
                    username = it.username,
                    petname = obj!!.petname,
                    img = obj.img,
                    description = it.description
                )
            )
        }

        return Resoult(200, momentList)
    }


    //新增朋友圈
    @Transactional
    @PostMapping("addNew")
    open fun addNew(moment: Moment): Resoult<String> {
        return if (momentServiceImpl.save(moment))
            Resoult.success("发布成功！")
        else
            Resoult.fail("发布失败！")

    }


    //删除朋友圈
    @Transactional
    @PostMapping("del")
    open fun del(@RequestParam("id") id: Int): Resoult<String> {
        return if (momentServiceImpl.removeById(id))
            Resoult.success("删除成功！")
        else
            Resoult.fail("删除失败！")
    }
}
